package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.feign.article.IArticleClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.StatisticsDto;
import com.heima.model.wemedia.pojos.WmStatistics;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vos.CountAllDateVo;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmStatisticsMapper;
import com.heima.wemedia.service.WmStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自媒体数据统计数据处理层
 *
 * @author 11494
 * @version 1.0
 * @project heima-leadnews-group
 * @date 2024/1/28 09:04:38
 */
@Service
@Transactional
@Slf4j
public class WmStatisticsServiceImpl extends ServiceImpl<WmStatisticsMapper, WmStatistics> implements WmStatisticsService {
    @Autowired
    private WmStatisticsMapper wmStatisticsMapper;
    @Autowired
    private IArticleClient iArticleClient;

    /**
     * @param beginDate :
     * @param endDate   :
     * @return ResponseResult
     * @author 18297
     * @date 2024/1/28 17:23
     * @description 章统计：图片发布量、文章点赞量、文章收藏量
     */
    @Override
    public ResponseResult newsDimension(String beginDate, String endDate) {
        // 参数校验
        if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"日期错误");
        }
        // 解析日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date bDate = new Date();
        Date eDate = new Date();
        try {
            bDate = sdf.parse(beginDate);
            eDate = sdf.parse(endDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 获取当前自媒体人id
        Integer wmUserId = WmThreadLocalUtil.getUser().getId();
        //远程调用方法，根据自媒体人的id去查询统计属于他对应的app端id的文章的对应数据
        ResponseResult responseResult = iArticleClient.countAllDate(wmUserId,bDate,eDate);
        // 准备vo封装数据返回
        CountAllDateVo countAllDateVo = new CountAllDateVo();
        // 返回200成功获取数据
        if (responseResult.getCode().equals(200)) {
            // 安全考虑远程调用返回的数据先转换为string类型，再反序列化为map，获取里面的数据
            String resStr = JSON.toJSONString(responseResult.getData());
            Map<String,Integer> map = JSON.parseObject(resStr, Map.class);
            //返回的数据赋值给vo
            countAllDateVo.setCollectNum(map.get("collections") == null ? 0 : map.get("collections"));
            countAllDateVo.setPublishNum(map.get("newsCount") == null ? 0 : map.get("newsCount"));
            countAllDateVo.setLikesNum(map.get("likes") == null ? 0 : map.get("likes"));
        }
        return ResponseResult.okResult(countAllDateVo);
    }

    /**
     * @author 18297
     * @date 2024/1/28 16:56
     * @description 分页展示文章列表,文章阅读、评论、收藏的数量
     * @param dto:
     * @return PageResponseResult
     */
    @Override
    public PageResponseResult newPage(StatisticsDto dto) {
        // 传入用当前自媒体用户的id去匹配app端文章表的作者id，查询属于他的文章的数据
        WmUser user = WmThreadLocalUtil.getUser();
        dto.setWmUserId(user.getId());
        return iArticleClient.newPage(dto);
    }
}
